package com.ruoyi.dynamic.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.dynamic.mapper.ParamMapper;
import com.ruoyi.dynamic.model.pojo.entity.Param;
import com.ruoyi.dynamic.service.IParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * <p>
 * 娱乐相关基础设置 服务实现类
 * </p>
 *
 * @author Gnc
 * @since 2024-12-25
 */
@Service
public class ParamServiceImpl extends ServiceImpl<ParamMapper, Param> implements IParamService {
    @Autowired
    private RedisService redisService;

    @PostConstruct
    public void init() {
        loadingConfigCache();
    }

    void loadingConfigCache() {
        for (Param param : list()) {
            redisService.setCacheObject(getCacheKey(param.getNameKey()), param.getValue());
        }
    }

    private String getCacheKey(String configKey) {
        return CacheConstants.SYS_PARAM + configKey;
    }


    public String selectConfigByKey(String configKey) {
        String configValue = Convert.toStr(redisService.getCacheObject(getCacheKey(configKey)));
        if (StringUtils.isNotEmpty(configValue)) {
            return configValue;
        }
        Param one = lambdaQuery()
                .eq(Param::getNameKey, configKey)
                .one();
        redisService.setCacheObject(getCacheKey(configKey), one.getValue());
        return StringUtils.EMPTY;
    }
}
